package org.footballmanager.pandafootball.system.controller.hy;

import org.footballmanager.pandafootball.common.constants.Constants;
import org.footballmanager.pandafootball.system.controller.BaseController;
import org.footballmanager.pandafootball.system.domain.FootSociaty;
import org.footballmanager.pandafootball.system.domain.FootSociatyBfinfo;
import org.footballmanager.pandafootball.system.domain.FootUser;
import org.footballmanager.pandafootball.system.domain.enums.BFZT;
import org.footballmanager.pandafootball.system.form.impl.FootSociatyBfinfoSearchForm;
import org.footballmanager.pandafootball.system.service.FootSociatyBfinfoService;
import org.footballmanager.pandafootball.system.service.FootSociatyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.support.SessionStatus;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

@Controller
@SessionAttributes("footSociatyBfinfoSearchForm")
@RequestMapping("/hy/footSociatyBfinfo")
public class HySociatyBfinfoController extends BaseController {
    /* 实体名称 */
    private static final String ENTITYNAME = "footSociatyBfinfo";
    private static final String LISTPAGE = String.format(Constants.LISTPAGE, Constants.HY, ENTITYNAME);
    private static final String ADDPAGE = String.format(Constants.ADDPAGE, Constants.HY, ENTITYNAME);

    @Autowired
    private FootSociatyBfinfoService footSociatyBfinfoService;
    @Autowired
    private FootSociatyService footSociatyService;

    /**
     * 初始化
     *
     * @param map                         参数
     * @param footSociatyBfinfoSearchForm 条件参数
     */
    @ModelAttribute
    public void init(Model map, @ModelAttribute FootSociatyBfinfoSearchForm footSociatyBfinfoSearchForm) {
        FootSociatyBfinfoSearchForm newFootSociatyBfinfoSearchForm = new FootSociatyBfinfoSearchForm();
        if (footSociatyBfinfoSearchForm != null) {
            newFootSociatyBfinfoSearchForm = footSociatyBfinfoSearchForm;
        }
        map.addAttribute(String.format(Constants.FORMNAME, ENTITYNAME), newFootSociatyBfinfoSearchForm);
    }

    @RequestMapping(value = "/search")
    public String search(Model map, @ModelAttribute FootSociatyBfinfoSearchForm footSociatyBfinfoSearchForm) {
        footSociatyBfinfoService.search(ENTITYNAME, map, footSociatyBfinfoSearchForm);
        return LISTPAGE;
    }

    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public String openAddPage(Model map, HttpServletRequest request) {
        Long userId = (Long) request.getSession().getAttribute(Constants.USERID);
        FootSociaty footSociaty = footSociatyService.findFootSociatyByUserId(userId);
        FootSociatyBfinfo bf = new FootSociatyBfinfo();
        bf.setZd1(footSociaty.getId());
        bf.setZd1Name(footSociaty.getName());
        bf.setBfzt(BFZT.NOT);
        map.addAttribute(ENTITYNAME, bf);
        return ADDPAGE;
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public String save(@Valid @ModelAttribute FootSociatyBfinfo footSociatyBfinfo,
                       BindingResult bindingResult, HttpServletRequest request, SessionStatus s) {
        if (!bindingResult.hasErrors()) {
            footSociatyBfinfoService.save(footSociatyBfinfo, request);
            s.setComplete();
            return "redirect:/hy/footSociatyBfinfo/view?id=" + footSociatyBfinfo.getId();
        } else {
            return ADDPAGE;
        }
    }

    @RequestMapping(value = "/mySearch")
    public String mySearch(Model map, HttpServletRequest request,
                           @ModelAttribute FootSociatyBfinfoSearchForm footSociatyBfinfoSearchForm) {
        FootUser user = (FootUser) request.getSession().getAttribute(Constants.USER);
        footSociatyBfinfoSearchForm.setBfqd(user.getId());
        footSociatyBfinfoSearchForm.setBfzt(BFZT.NOT);
        footSociatyBfinfoService.search(ENTITYNAME, map, footSociatyBfinfoSearchForm);
        return "hy/bf/footSociatyBfinfo/MyList";
    }

    @RequestMapping(value = "/view", method = RequestMethod.GET)
    public String openViewPage(Model map, @RequestParam Long id, HttpServletRequest request,
                               @ModelAttribute FootSociatyBfinfoSearchForm footSociatyBfinfoSearchForm) {
        return footSociatyBfinfoService.view(id, map, request, footSociatyBfinfoSearchForm, true);
    }

    @RequestMapping(value = "/myView", method = RequestMethod.GET)
    public String openMyViewPage(Model map, @RequestParam Long id, HttpServletRequest request,
                                 @ModelAttribute FootSociatyBfinfoSearchForm footSociatyBfinfoSearchForm) {
        return footSociatyBfinfoService.view(id, map, request, footSociatyBfinfoSearchForm, false);
    }

    @RequestMapping(value = "/makesure", method = RequestMethod.POST)
    public String makesure(@ModelAttribute FootSociatyBfinfo footSociatyBfinfo, HttpServletRequest request, SessionStatus s) {
        footSociatyBfinfoService.makesure(footSociatyBfinfo.getId(), request);
        s.setComplete();
        return "redirect:/hy/footSociatyBfinfo/view?id=" + footSociatyBfinfo.getId();
    }

    @RequestMapping(value = "/searchSociaty", method = RequestMethod.POST)
    @ResponseBody
    public List<FootSociaty> searchSociaty() {
        return footSociatyService.getall();
    }

}
